package com.meepo.feed.feed

import android.content.Context
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.meepo.basic.Consts
import com.meepo.basic.components.ImageLoaderFactory
import com.meepo.basic.components.ImagePopup
import com.meepo.basic.router.RouterManager
import com.meepo.basic.schema.Enums
import com.meepo.basic.schema.feed.Item
import com.meepo.basic.schema.feed.Resource
import com.meepo.basic.schema.feed.Tag
import com.meepo.basic.schema.feed.Topic
import com.meepo.basic.schema.user.User
import com.meepo.feed.R
import com.meepo.feed.video.fullscreen.FullSnVideoListActivity
import com.meepo.framework.design.desc.Data
import com.meepo.framework.design.desc.DescLayout
import com.meepo.framework.design.helper.ColorHelper
import com.meepo.framework.design.helper.ImageLoader
import com.meepo.framework.design.recycler.LinearAdapter
import com.meepo.framework.design.recycler.ViewHolder
import com.meepo.framework.design.tag.TagView

object ItemRender {

    fun renderAuthor(context: Context, descLayout: DescLayout, author: User?) {
        descLayout.setData(
            Data(
                title = author?.nickname ?: "",
                avatar = author?.avatar ?: ""
            )
        )

        descLayout.setOnClickListener {
            author?.id?.let {
                RouterManager.UserModule.routeToUserProfile(context, it)
            }
        }
    }

    fun renderTags(context: Context, recyclerView: RecyclerView, tags: List<Tag>?) {
        tags?.let {
            recyclerView.layoutManager = LinearLayoutManager(context, RecyclerView.HORIZONTAL, false)
            recyclerView.adapter = object : LinearAdapter<Tag>(context, it) {
                override fun getItemLayoutId(item: Tag, position: Int): Int = R.layout.item_tag

                override fun bind(item: Tag, holder: ViewHolder, position: Int) {

                    holder.getView<TagView>(R.id.tag_view)?.let { bt ->
                        bt.text = item.name
                        bt.setTextColor(ColorHelper.parseColor(item.color))
                    }
                }

            }
        }
        if (tags.isNullOrEmpty()) {
            recyclerView.visibility = View.GONE
        }
    }

    // todo 优化
    fun renderCovers(
        context: Context,
        item: Item,
        recyclerView: RecyclerView,
        singleImageLayout: LinearLayout,
        imageView: ImageView,
        videoCtrlView: ImageView
    ) {

        val imageLoader = ImageLoaderFactory.getImageLoader(context, 500)

        if (item.getCovers().isEmpty()) return

        item.getCovers().let { covers ->
            when (item.getViewType()) {
                Enums.VIEW_TYPE_SMALL_LIST -> {

                    recyclerView.visibility = View.VISIBLE

                    recyclerView.layoutManager = GridLayoutManager(context, Consts.Feed.MAX_IMAGE_COUNT_PER_ROW)
                    recyclerView.itemAnimator = null // 取消更新闪烁

                    recyclerView.adapter = object : LinearAdapter<Resource>(context, covers) {
                        override fun getItemLayoutId(item: Resource, position: Int): Int {
                            return R.layout.feed_list_item_multi_image_item_layout
                        }

                        override fun bind(item: Resource, holder: ViewHolder, position: Int) {
                            holder.setImagePath(R.id.cover_iv, item.content, imageLoader)
                            holder.getView<ImageView>(R.id.cover_iv)?.let { imageView ->
                                imageView.setOnClickListener {
                                    ImagePopup.popup(context, imageView, item.content)
                                }

                            }
                        }
                    }
                }
                else -> {
                    singleImageLayout.visibility = View.VISIBLE

                    ImageLoader.loadImage(context, covers[0].content ?: "", imageView)

                    if (item.content?.isVideo() == true) {
                        videoCtrlView.visibility = View.VISIBLE
                        imageView.setOnClickListener {
                            FullSnVideoListActivity.launch(context, arrayListOf(item))
                        }
                    } else {
                        imageView.setOnClickListener {
                            ImagePopup.popup(context, imageView, covers[0].content)
                        }
                    }
                }
            }
        }
    }

    fun renderTopic(context: Context, textView: TextView, topic: Topic?) {
        textView.text = topic?.title

        topic?.let { tp ->
            textView.setOnClickListener {
                RouterManager.FeedModule.routeToFeedTopicDetail(context, tp.id)
            }
        }
    }


}